﻿package net.cooltain.warhammer.running.start;

import java.util.HashMap;
import java.util.Map;

import net.cooltain.utils.Logger;
import net.cooltain.warhammer.framework.constant.ConstantGame;
import net.cooltain.warhammer.running.entity.MotionCard;
import net.cooltain.warhammer.running.entity.MotionPlayerZone;

/**
 * 游戏桌面
 * @author cooltian
 */
public class GameBoardDouble {
	
	
	// 所有玩家,所有卡牌列表
	public static Map<Integer,MotionCard> allMotionCards = new HashMap<Integer,MotionCard>(200);
	
	static Integer countNum = 0;
	public static void geneterMotionCard(MotionCard card){
		int random = (int) (Math.random()*100);
		Integer uniqueKey = (++countNum)*100+random;;
		card.uniqueKey = uniqueKey ;
		allMotionCards.put(uniqueKey, card);
	}

	private boolean isGameRunning = false;
	public boolean isGameRunning() {
		return isGameRunning;
	}

	// 双方玩家
	public MotionPlayerZone[] players = new MotionPlayerZone[2];
	
	
	public void loadUser(MotionPlayerZone player){
		if(player==null){
			System.out.println("WARNNING:载入的玩家为null");
			return ;
		}
		// TODO 
		if(players[0]==null && players[1]==null){
			players[0] = player ;
			players[0].gameBoard = this;		
			System.out.println(player.playerName+"加入了游戏");
			return ;
		}else if(players[0]!=null && players[1]!=null){
			System.out.println("已经有两个玩家了,不能再加入了");
			return ;
		}else{
			if( player.equals(players[0]) || player.equals(players[1]) ){
				System.out.println("玩家"+player.playerName+"已经加入了,不能重复操作");
				return ;
			}
			if(players[1]==null){
				players[1] = player ;
				players[1].gameBoard = this;
				players[1].operatePlayer = players[0];
				players[0].operatePlayer = players[1];		
			}else{
				players[0] = player ;
				players[0].gameBoard = this;
				players[0].operatePlayer = players[1];
				players[1].operatePlayer = players[0];				
			}
			System.out.println(player.playerName+"加入了游戏");
		}
	}
	
	
	/**
	 * 游戏开始
	 */
	public void startGame(){
		if(isGameRunning){
			System.out.println("游戏已经开始了，不能重复操作");
		}else if(players[0]!=null && players[1]!=null){
			System.out.println("游戏开始");
			players[0].deskZone.clearAllVisible();
			players[1].deskZone.clearAllVisible();
			players[0].deskZone.shuffleDesk();
			players[1].deskZone.shuffleDesk();
			randomFirstPlayer();
			players[0].deskZone.drawTopCard(ConstantGame.GAME_START_DRAW_CARD_NUM);
			players[1].deskZone.drawTopCard(ConstantGame.GAME_START_DRAW_CARD_NUM);
			isGameRunning = true;
		}else{
			isGameRunning = false;
			System.out.println("玩家不够,无法开始");
		}
	}
	
	/**
	 * 重新开始游戏
	 */
	public void restartGame(){
		// TODO 
	}
	
	/**
	 * 随机设置先手玩家
	 */
	private void randomFirstPlayer(){
		int i = (int) (Math.random()*2);
		i = 0; // TODO: delete this row
		players[i].isFisrtPlay = true ;
		players[i].operatePlayer.isFisrtPlay = false;
		players[i].isMyTurn = true;
		players[i].operatePlayer.isMyTurn = false;
		Logger.info(players[i].playerName+" 先攻");
	}

}
